package ProtoSimulator;
/**
 * 
 */

/**
 * @author jjmb
 *
 */

import java.io.*;
import java.net.*;
import java.lang.Thread;
import java.net.URL;
import java.util.HashMap;

import org.apache.log4j.Logger;

public class Status {
	private static final String CLASS_NAME = Status.class.getName();
    public static final Logger logger = Logger.getLogger(CLASS_NAME);
    
	private static HashMap<Long, Long> simulators = new HashMap<Long, Long>();
	private long elapsedTime = 0;
	private boolean complete = false;
	private int iterationComplete = 0;
	private int iterationMaximum = 0;
	private boolean available = false;

	/**
   public Status() {
   }
   **/
	
   // use this to get the elapsed time for one simulator
   public synchronized Object getIterationElapsedTime(int id) {
	   logger.trace("Getting iteration for ID, " + id);
	   while (available == false) {
		    try {
		    	wait();
		    } 
		    catch (InterruptedException e) {
		    }
		}
        available = false;
		notify();
		// iterationElapsedTime = simulators.get(id);
		logger.trace("Returning, " + simulators.get(id).toString());
		return simulators.get(id);
   }
	
   // use this to set the elapsed time for one simulator
   public synchronized void setIterationElapsedTime(long id, long elapsedTime) {
	   logger.trace("Setting elapsed time (" +elapsedTime+ ") for ID " + id + " (available = "+available+")");
	   while (available == true) {
	        try {
	        	wait();
	        }
	        catch (InterruptedException e) {
	        }
	   }
	   available = true;
	   simulators.put(id, elapsedTime);
	   logger.trace("Size of simulators hash table after putting values " + simulators.size());
	   this.elapsedTime = this.elapsedTime + elapsedTime;
	   logger.trace("Elapsed time now equals, " + this.elapsedTime);
	   available = false;
	   notify();
   }
   
   public synchronized void incrementIterationsComplete() {
	   logger.debug("Incrementing iterations completed");
	   while (available == true) {
	        try {
	        	wait();
	        }
	        catch (InterruptedException e) {
	        }
	   }
	   available = true;
	   iterationComplete++;
	   logger.trace("Iterations complete set to " + iterationComplete);
	   available = false;
	   notify();
   }
   
   public synchronized void setIterationMaximum(int maximum) {
	   logger.trace("Setting maximum iterations to " + maximum);
	   iterationMaximum = maximum;
   }
	
   // use  this to get the overall elapsed time
	public synchronized long getElapsedTime() {
		logger.trace("Size of simulator hash table " + simulators.size() + ", maximum iterations " + iterationMaximum);
		while (simulators.size() < iterationMaximum) {
	        try {
	        	wait();
	        } 
	        catch (InterruptedException e) {
	        }
		}
		available = false;
		notify();
		logger.trace("Elapsed time being returned, " + elapsedTime);
		return elapsedTime;
	}
	
	public synchronized boolean isComplete() {
		logger.trace("Iterations complete, " + iterationComplete + ", maximum iterations " + iterationMaximum);
		while (iterationComplete < iterationMaximum) {
	        try {
	        	wait();
	        } 
	        catch (InterruptedException e) {
	        }
		}
		complete = true;
		available = false;
		notify();
		logger.trace("Retuning complete status " + complete);		
		return complete;
	}
}